#!/bin/bash


Redecho()
{
        printf '\033[31m%s\033[0m\n' "$1"
}
Purecho()
{
        printf '\033[35m%s\033[0m\n' "$1"
}




Install_Python()
{
	# 判断是否安装python3
	if command -v python3 >/dev/null 2>&1; then
    Purecho "python3已经安装,将开始下一步操作."
  else
    Purecho "python3没有安装 ，将开始安装python3版本"
    if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
		  PM="yum"
	  elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
		  PM="apt-get"
	  fi

	  if [ "${PM}" = "yum" ];then
		  yum -y install python3
	  elif [ "${PM}" = "apt-get" ];then
		  apt-get -y install python3
	  fi
  fi

}



Install_pip()
{
    required_version="20.0"
    current_version=$(pip --version | awk '{print $2}')
    current_version_major=$(echo $current_version | awk -F. '{print $1}')   # 主版本
    required_version_major=$(echo $required_version | awk -F. '{print $1}') # 小版本
    if [ $current_version_major -lt $required_version_major ] ; then
        Purecho "当前pip版本过低，需要升级到至少 ${required_version} 版本。"
        Purecho "开始自动升级"
        pip3 install --upgrade pip
    else
      Purecho "pip版本满足要求,将开始下一步操作"
    fi
}



Install_invoke()
{
	invoke_info=$(pip3 show invoke | awk '{print $2}')
	if [ -n "$invoke_info" ];then
	  Purecho "invoke模块已经安装，将继续下一步操作"
	else
	  Purecho "invoke模块没有安装，将开始安装该模块"
	  pip3 install invoke
	fi

}

Install_docker_compose()
{
	docker_compose_info=$(pip3 show docker-compose | awk '{print $2}')
	if [ -n "$docker_compose_info" ];then
	  Purecho "docker-compose模块已经安装，将继续下一步操作"
	else
	  Purecho "docker-compose模块没有安装，将开始安装该模块"
	  pip3 install docker-compose -i https://pypi.tuna.tsinghua.edu.cn/simple/
	fi

}



Build_docker_images()
{
    Purecho "开始构建镜像文件"
    docker rmi -f auto_test:latest
    HOST_IP=$(hostname -I | awk '{print $1}')
    export HOST_IP=$HOST_IP
    printenv | grep -E HOST_IP > .env
    docker build -t auto_test .

}




Install_Python
Install_pip
Install_invoke
Install_docker_compose
Build_docker_images






